package com.thirteenClassicsServer.controller;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.thirteenClassics.constant.MessageConstant;
import com.thirteenClassics.dto.PictureCatalogQueryDTO;
import com.thirteenClassics.entity.Catalog;
import com.thirteenClassics.result.Result;
import com.thirteenClassics.utils.AliOssUtil;
import com.thirteenClassicsServer.service.impl.FileServiceImpl;
import com.thirteenClassicsServer.service.impl.PictureServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private FileServiceImpl fileService;

    @Autowired
    private PictureServiceImpl pictureService;

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传：{}", file);

        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID() + extension;

            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }

        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

    @PostMapping("/uploadBook")
    @ApiOperation("文件上传")
    public Result<String> uploadBook(MultipartFile file) {
        log.info("文件上传：{}", file);

        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID() + extension;

            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }

        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

    @GetMapping("/showCatalogs")
    @ApiOperation("获取图书目录列表")
    public Result<List<Catalog>> showCatalogs(@RequestParam("bookName") String bookName,
            @RequestParam("bookLang") String bookLang) {
        try {
            log.info("获取图书目录列表：{}", bookName);

            // 参数验证
            if (bookName == null || bookName.isEmpty()) {
                return Result.error("参数错误：图书名不能为空");
            }

            PictureCatalogQueryDTO pictureCatalogQueryDTO = new PictureCatalogQueryDTO();
            pictureCatalogQueryDTO.setBookName(bookName);
            pictureCatalogQueryDTO.setBookLang(bookLang);

            List<Catalog> catalogList = pictureService.selectCatalogList(pictureCatalogQueryDTO);
            for (int i = 1; i <= catalogList.size(); i++) {
                catalogList.get(i - 1).setOrders((long) i);
            }

            return Result.success(catalogList);
        } catch (Exception e) {
            log.error("获取图书目录列表失败：{}", e.getMessage());
            return Result.error("获取图书目录列表失败，请稍后重试");
        }
    }

    @GetMapping("/showCatalogsJdb")
    @ApiOperation("获取图书目录列表")
    public Result<List<Catalog>> showCatalogsJdb(@RequestParam("bookName") String bookName) {
        try {
            log.info("获取图书目录列表：{}", bookName);

            // 参数验证
            if (bookName == null || bookName.isEmpty()) {
                return Result.error("参数错误：图书名不能为空");
            }

            PictureCatalogQueryDTO pictureCatalogQueryDTO = new PictureCatalogQueryDTO();
            pictureCatalogQueryDTO.setBookName(bookName);

            List<Catalog> catalogList = pictureService.selectCatalogListJdb(pictureCatalogQueryDTO);

            return Result.success(catalogList);
        } catch (Exception e) {
            log.error("获取图书目录列表失败：{}", e.getMessage());
            return Result.error("获取图书目录列表失败，请稍后重试");
        }
    }


    //    @PostMapping("/uploadCatalog")
    //    @ApiOperation("excel文件导入目录结构")
    //    public Result<String> uploadCatalog(MultipartFile file) throws Exception {
    //        log.info("excel文件导入：{}",file);
    //
    //        fileService.excelImport(file);
    //
    //        return Result.success();
    //    }

}
